﻿using BBICMS.Application.ViewModels;
using BBICMS.Infrastructure.UI;
using BBICMS.Model;
using BBICMS.Model.ViewModel.Back;
using BBICMS.Resources;
using BBICMS.WebUI.Filters;
using System.Linq;
using System.Web.Mvc;

namespace BBICMS.WebUI.Areas.Admin.Controllers
{
    [SIGAuth]
    
    public class CarouselController : BaseController
    {
        //private ApplicationUnit _unit = new ApplicationUnit();
        //
        // GET: /Admin/Carousel/

        public ActionResult Index(int page = 1)
        {
            int PageSize = BBICMS.Infrastructure.Helpers.Settings.PageSize;

            CarouselListVM CarouselList = new CarouselListVM
            {
                Carousels = _unit.Carousels.GetAll().OrderByDescending(s => s.Importance)
                    .Skip((page - 1) * PageSize)
                    .Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = _unit.Carousels.GetAll().Count()
                }
            };

            return View(CarouselList);
        }

        
        [HttpGet]
        public ActionResult AddCarousel()
        {
            Carousel vCarousel = new Carousel();
            vCarousel.Active = true;
            vCarousel.Importance = 0;

            return View("AddCarousel", vCarousel);
        }


        
        [HttpPost]
        public JsonResult AddCarousel(Carousel carousel)
        {
            
            if (ModelState.IsValid)
            {
                _unit.Carousels.Add(carousel);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Carousel));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertCreateFailure, EntityNames.Carousel));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }

       
        public ActionResult EditCarousel(int Id)
        {
            var vCarousel = _unit.Carousels.GetById(Id);
            if (vCarousel != null)
            {
                return View(vCarousel);
            }
            return View("NotFound");
        }

        [HttpPost]

        public JsonResult EditCarousel(Carousel Carousel)
        {


            //carousel.UpdatedBy = User.Identity.Name;
            //carousel.UpdatedDate = DateTime.Now;   

            if (ModelState.IsValid)
            {
                _unit.Carousels.Update(Carousel);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Carousel));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.Carousel));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }

        // DELETE: /User/DeleteCarousel
        [HttpPost]
        public JsonResult DeleteCarousel(int id)
        {

            Carousel vCarousel = _unit.Carousels.GetById(id);

            if (vCarousel != null)
            {
                string imgurl = Server.MapPath(vCarousel.ImageUrl);
                if (System.IO.File.Exists(imgurl))
                {
                    System.IO.File.Delete(imgurl);    //删除缩略图
                }

                _unit.Carousels.Delete(vCarousel);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertDeleteSuccess, EntityNames.Carousel));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertDeleteFailure, EntityNames.Carousel));
            return Json(AR, JsonRequestBehavior.DenyGet);
        }

    }
}
